<template>
  <div class="container">
    <div class="article-detail">
      <div class="title">{{detail.title}}</div>
      <img class="img" :src="detail.image|imgPath" alt>
      <div class="date">2019年03月06日07:20 来源：人民网－人民日报</div>
      <div class="content" v-html="detail.content"></div>
    </div>
    <div class="btns">
      <div class="left">
        <span @click="onDownload">
          <van-icon name="upgrade" color="#e71f19"/>
          <span>文件下载</span>
        </span>
      </div>
      <div class="right">
        <span>
          <van-icon name="star-o" color="#e71f19"/>
          <span>收藏</span>
        </span>
        <span>
          <van-icon name="share" color="#e71f19"/>
          <span>分享</span>
        </span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      id: '',
      detail: {}
    }
  },
  created () {
    const id = this.$route.params.id
    if (id) {
      this.id = id
      this.getDetail()
      this.reportProcess()
    }
  },
  methods: {
    reportProcess () {
      this.$axios.post('/xbkj-dj/web/edu/course/learn/report', {
        id: this.id
      })
    },
    onDownload () {
      const ecas = this.detail.ecas[0]
      if (ecas && ecas.resourceLinkPath) {
        var a = document.createElement('a')
        a.setAttribute('href', ecas.resourceLinkPath)
        a.click()
      } else {
        this.$toast('暂无资源')
      }
    },
    getDetail (callback) {
      this.$axios
        .get('/xbkj-dj/web/edu/course/detail', {
          id: this.id
        })
        .then(res => {
          let detail = res.data
          detail.content = detail.content
            ? decodeURIComponent(detail.content).replace(
              /\<img/gi,
              '<img style="width:100%;height:auto" '
            )
            : ''
          this.detail = detail
        })
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  height: 100%;
}
.article-detail {
  background-color: #fff;
  padding-bottom: 60px;
  min-height: 100%;
  box-sizing: border-box;
  .title {
    font-size: 15px;
    font-weight: bold;
    text-align: center;
    padding: 10px 0;
  }
  .date {
    font-size: 12px;
    color: #999;
    text-align: center;
    margin-bottom: 10px;
  }
  .img {
    display: block;
    width: 100%;
    margin-bottom: 10px;
  }
  .content {
    padding: 0 20px;
  }
}
.btns {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  margin-top: -50px;
  .left {
    .van-icon {
      transform: rotate(180deg);
    }
    > span {
      display: flex;
      align-items: center;
    }
  }
  .right {
    display: flex;
    > span {
      display: flex;
      align-items: center;
      margin-left: 20px;
    }
  }
}
</style>
